<?php

namespace App\Admin\Extensions;

use Encore\Admin\Grid\Column;
use Encore\Admin\Grid\Exporters\AbstractExporter;
use Maatwebsite\Excel\Facades\Excel;
use App\Admin\Controllers\Squad_monthController;
use Encore\Admin\Widgets\Table;

class Squad_monthExpoter extends AbstractExporter
{
    public $titles = [];

    public $id;

    private $class;

    public function __construct($grid, $name,$id)
    {
        $this->grid = $grid;
        $this->tablename = $name;
        $this->id = $id;
        $this->class = new Squad_monthController();
    }

    public function export()
    {
        Excel::create($this->tablename . date('YmdHis'), function ($excel) {
            // $excel->setTitle('这是啥');
            // $excel->setCreator('Creator这又是啥')
            //     ->setCompany('Maatwebsite又是什么鬼');
            // $excel->setDescription('Description这个好像挺长，写点啥呢');
            $columns = $this->grid->columns();
            if (!empty($columns)) {
                foreach ($columns as $c) {
                    array_push($this->titles, $c->getLabel());
                }
            }
            $excel->sheet($this->tablename, function ($sheet) {
                $this->grid->build();
                // 这段逻辑是从表格数据中取出需要导出的字段
                $dbcolumnnames = [];
                $titlenames = [];
                $this->grid->columns()->map(function (Column $column) use (&$dbcolumnnames, &$titlenames) {
                    if ($column->getName() != '__row_selector__') {
                        array_push($dbcolumnnames, $column->getName());
                        array_push($titlenames, $column->getLabel());
                    }
                });
                $dbcolumnnames = array_unique($dbcolumnnames);
                $titlenames = array_unique($titlenames);
                $sheet->rows([$titlenames]);

                $rows = $this->grid->rows();
                $datas = $rows->map(function ($item) use ($dbcolumnnames) {
                    $row = array();
                    $model = $item->model();
                    foreach ($dbcolumnnames as $key) {
                        $row[$key] = $this->cutstr_html($model[$key]);
                    }
                    return $row;                 
                });
                $sheet->rows($datas);
            });

            $data = $this->class->get_every_data($this->id);
            array_unshift($data,['类型','班段','开班批次','分组','班型','全勤率平均','家校准交率平均','作业准交率平均','流失率平均','流失人数总和']);
            $excel->sheet('各种平均',function($sheet) use ($data){
                $sheet->rows($data);
            });

        })->export('xls');
    }


    //去掉文本中的HTML标签
    public function cutstr_html($string, $length = 0, $ellipsis = '…')
    {
        $string = strip_tags($string);
        $string = preg_replace('/\n/is', '', $string);
        $string = preg_replace('/ |　/is', '', $string);
        $string = preg_replace('/&nbsp;/is', '', $string);
        preg_match_all("/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/", $string, $string);
        if (is_array($string) && !empty($string[0])) {
            if (is_numeric($length) && $length) {
                $string = join('', array_slice($string[0], 0, $length)) . $ellipsis;
            } else {
                $string = implode('', $string[0]);
            }
        } else {
            $string = '';
        }
        return $string;
    }
}
